﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Azure.Logger;

namespace Azure.Core
{
    internal class AzBlockTarget
    {
        private String _name = String.Empty;
        private List<AzTarget> _targetList = null;

        public String Name 
        {
            get
            {
                return _name;
            }
        }

        public List<AzTarget> TargetList 
        {
            get
            {
                return _targetList;
            }
        }

        public AzBlockTarget(String name, int targetCount)
        {
            _name = name;
            _targetList = new List<AzTarget>(targetCount);
        }

        public void AddTarget(AzTarget target)
        {
            _targetList.Add(target);
        }
    }

    internal class AzTarget
    {
        private String _name = String.Empty;
        private AzFormula _formula = null;
        private Object _defaultValue = null;
        private Type _type = null;
        private Object _value;

        public String Name { get { return _name; } }

        public AzFormula Formula { get { return _formula; } }

        public Object DefaultValue { get { return _defaultValue; } }

        public Type Type { get { return _type; } }

        public Object Value { get { return _value; } }

        public AzTarget(String name, String type, String defaultValue, AzFormula formula)
        {
            _name = name;
            _formula = formula;
            _defaultValue = defaultValue;

            //创建对应的TYPE
            try
            {
                _type = System.Type.GetType(type);

                if (_type == null)
                {
                    throw new Exception("Target " + name + " 的数据类型" + type +"不是有效的类型");
                }
            }
            catch (Exception e)
            {
                AzLogger.Error("Target " + Name + " 的数据类型不是有效的类型", e);
                throw;
            }

            //创建Value的实例
            //无奈String 类型没有无参的构造函数
            if (Type == typeof(String))
            {
                _value = String.Empty;
                _defaultValue = String.Empty;
            }
            else
            {
                _value = Activator.CreateInstance(Type);
                _defaultValue = Activator.CreateInstance(Type);
            }

            //检验DefaultValue的格式是否正确
            if (!Parse(defaultValue, ref _defaultValue))
            {
                AzLogger.Error("Target " + Name + " defaultValue的值不是一个有效的" + _type.ToString() + "类型，将使用系统默认值");
            }
        }

        public void ParseValue(String value)
        {
            if (!Parse(value, ref _value))
                _value = _defaultValue;
        }

        private bool Parse(String value, ref Object targetObject)
        {
            #region 校验value是不是和type兼容
            bool ret = false;

            if (targetObject is System.Int16)
            {
                Int16 int16;
                if (!System.Int16.TryParse(value, out int16))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Int16, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = int16;
                }
            }
            else if (targetObject is System.Int32)
            {
                Int32 int32;
                if (!System.Int32.TryParse(value, out int32))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Int32, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = int32;
                }
            }
            else if (targetObject is System.Int64)
            {
                Int64 int64;
                if (!System.Int64.TryParse(value, out int64))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Int64, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = int64;
                }
            }
            else if (targetObject is System.Byte)
            {
                Byte b;
                if (!System.Byte.TryParse(value, out b))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Byte, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = b;
                }
            }
            else if (targetObject is System.UInt16)
            {
                UInt16 uint16;
                if (!System.UInt16.TryParse(value, out uint16))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.UInt16, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = uint16;
                }
            }
            else if (targetObject is System.UInt32)
            {
                UInt32 uint32;
                if (!System.UInt32.TryParse(value, out uint32))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.UInt32, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = uint32;
                }
            }
            else if (targetObject is System.UInt64)
            {
                UInt64 uint64;
                if (!System.UInt64.TryParse(value, out uint64))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.UInt64, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = uint64;
                }
            }
            else if (targetObject is System.SByte)
            {
                SByte sb;
                if (!System.SByte.TryParse(value, out sb))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.SByte, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = sb;
                }
            }
            else if (targetObject is System.Single)
            {
                Single single;
                if (!System.Single.TryParse(value, out single))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Single, 将使用0作为默认值");
                    targetObject = 0.0f;
                }
                else
                {
                    ret = true;
                    targetObject = single;
                }
            }
            else if (targetObject is System.Double)
            {
                Double db;
                if (!System.Double.TryParse(value, out db))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Double, 将使用0作为默认值");
                    targetObject = 0.0d;
                }
                else
                {
                    ret = true;
                    targetObject = db;
                }
            }
            else if (targetObject is System.Boolean)
            {
                Boolean boolean;
                if (!System.Boolean.TryParse(value, out boolean))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Boolean, 将使用false作为默认值");
                    targetObject = false;
                }
                else
                {
                    ret = true;
                    targetObject = boolean;
                }
            }
            else if (targetObject is System.DateTime)
            {
                DateTime dateTime;
                if (!System.DateTime.TryParse(value, out dateTime))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.DateTime, 将使用DateTime.MinValue作为默认值");
                    targetObject = DateTime.MinValue;
                }
                else
                {
                    ret = true;
                    targetObject = dateTime;
                }
            }
            else if (targetObject is System.Decimal)
            {
                Decimal deci;
                if (!System.Decimal.TryParse(value, out deci))
                {
                    AzLogger.Error("Target " + Name + " 的默认值是个无效的System.Decimal, 将使用0作为默认值");
                    targetObject = 0;
                }
                else
                {
                    ret = true;
                    targetObject = deci;
                }
            }
            else if (targetObject is System.String)
            {
                targetObject = value;
                ret = true;
            }
            else
            {
                AzLogger.Error("Target " + Name + " 的数据类型不是有效的类型");
            }

            return ret;
            #endregion
        }
    }
}
